﻿Imports PadovadoAPI
Imports System.Data

Partial Class AdminNews
    Inherits System.Web.UI.Page

    Public Property PK_NewsCorrente As Integer
        Set(value As Integer)
            ViewState("PK_NewsCorrente") = value
        End Set
        Get
            If IsNothing(ViewState("PK_NewsCorrente")) Then ViewState("PK_NewsCorrente") = New Integer
            Return ViewState("PK_NewsCorrente")
        End Get
    End Property

    Protected Sub BtnSalva_Click(sender As Object, e As System.EventArgs) Handles BtnSalva.Click
        Dim sql As String = ""
        Dim dbo As New DBObject

        ' Dim PK_News As Integer
        Try

            Dim nw As New News
            nw.PK_News = PK_NewsCorrente
            nw.Text = TBTestoNotizia.Value
            nw.Priorita = TBPriorita.Text
            nw.DataInizioValidita = CntlData1.DataInizio
            nw.DataFineValidita = CntlData1.DataFine
            If (nw.PK_News = 0) Then
                nw.DataInserimento = Date.Now
            End If
            nw.Salva()

            caricaGrid()

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If String.IsNullOrEmpty(Session("UserInSession")) Then
            Response.Redirect("Admin.aspx")
        End If

        caricaGrid()
    End Sub

    Public Sub caricaGrid()
        Dim sql As String = "SELECT PK_News, Text, DataInizioValidita, DataFineValidita, DataInserimento FROM News WHERE Deleted = 0 ORDER BY Priorita desc, PK_News desc"
        Dim ds As New DataSet
        Dim dbo As New DBObject
        ds = dbo.FillDS(sql, Nothing, CommandType.Text)
        If Not IsNothing(ds) AndAlso (ds.Tables.Count > 0) AndAlso ds.Tables(0).Rows.Count > 0 Then
            GVNews.DataSource = ds.Tables(0)
            GVNews.DataBind()
        End If
    End Sub

    Public Sub caricaRiga(nw As News)
        TBTestoNotizia.Value = nw.Text
        TBPriorita.Text = nw.Priorita
        'Throw New Exception(ev.Descrizione & " " & ev.NomeEvento)
        TBPriorita.Text = nw.Priorita
     

        CntlData1.DataInizio = nw.DataInizioValidita
        CntlData1.DataFine = nw.DataFineValidita
 
    End Sub

    Protected Sub GVNews_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GVNews.RowCommand
        If Not IsNothing(e) Then

            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = GVNews.Rows(index)
            PK_NewsCorrente = row.Cells(0).Text

            If e.CommandName = "CaricaRiga" Then


                Dim nw As New News
                nw.LoadFromPk(PK_NewsCorrente)
                caricaRiga(nw)
            ElseIf e.CommandName = "EliminaRiga" Then

                News.Elimina(PK_NewsCorrente)

            End If
        End If

    End Sub

    Protected Sub GVNews_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GVNews.PageIndexChanging

        GVNews.PageIndex = e.NewPageIndex

        GVNews.DataBind()
    End Sub

    Protected Sub BtnSvuota_Click(sender As Object, e As System.EventArgs) Handles BtnSvuota.Click
        TBTestoNotizia.Value = ""
        TBPriorita.Text = "100"

        CntlData1.DataInizio = DateTime.Now.Date
        CntlData1.DataFine = DateTime.Now.Date
        PK_NewsCorrente = 0
    End Sub
End Class
